/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.farmcode.plugin.objectinfo.jdbc;

import com.farmcode.api.runtime.FARMCodeContext;
import com.farmcode.api.types.Type;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author oiprado
 */
public class DatabaseInfo <T, J> implements DatabaseObjectInfo<T, J> {
  
  protected FARMCodeContext context;
  private Connection connection;
  private HashMap<Integer, Type> types;
  
  public DatabaseInfo(String driver) throws ClassNotFoundException {
    Class.forName(driver);
  }
  
  public DatabaseInfo(FARMCodeContext context) throws ClassNotFoundException {
    this((String)context.get(FARMCodeContext.DRIVER));
    types = ((HashMap<Integer, Type>)context.get(FARMCodeContext.TYPES));
    this.context = context;
  }

  @Override
  public List<T> explore() throws SQLException{
    throw new UnsupportedOperationException("Not supported yet.");
  }
  
  @Override
  public J exploreChilds(J dbo) throws SQLException{
    throw new UnsupportedOperationException("Not supported yet.");
  }

  /**
   * @return the connection
   */
  protected Connection getConnection() throws SQLException {
    if(context != null){
      connection = DriverManager.getConnection
      (
        (String)context.get(FARMCodeContext.HOST), 
        (String)context.get(FARMCodeContext.USER), 
        (String)context.get(FARMCodeContext.PASSWORD)
      );
    }
    return connection;
  }
  
  public  boolean test() throws SQLException{
        
        getConnection();
        Logger.getLogger(DatabaseInfo.class.getName(),null).log(Level.INFO, "Active: {0}", !connection.isClosed());
        
      return true;
  }

  /**
   * @param connection the connection to set
   */
  protected void setConnection(Connection connection) {
    this.connection = connection;
  }

  @Override
  public void setTypes(HashMap<Integer, Type> types) {
    this.types = types;
  }

  @Override
  public HashMap<Integer, Type> getTypes() {
    return types;
  }
  
}
